#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <limits>
#include "parse.hpp"

typedef double(*double_fun)(const double&); // define type "double_fun"

double trapz1(const double_fun& f, 
	      const double& a, const double& b, 
	      const size_t N){
	double h = (b-a)/N;
	double sum = (f(a)+f(b))/2;
	for(size_t i=1; i<N; i++){
		sum += f(a+i*h);
	}
	return h*sum;
}
double x2(const double& x)
{
	return x*x;
}
int main(int argc, char *argv[]) {
	double exact = 1/3.0;
	size_t N = 128;
	if(argc > 1) parse(argv[1], N);
	std::cout
		<< std::setprecision(std::numeric_limits<long double>::digits10+1)
		<< std::scientific;
	long double Q = trapz1(x2, 0, 1, N);
	std::cout
		<< N << "\t"
		<< Q << "\t"
		<< std::abs(Q - exact) // absolute error
		<< std::endl;

}



